package com.elean.pms.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * druid 配置 这样的方式不需要添加注解：@ServletComponentScan
 * http://localhost/frame/druid/index.html 访问
 * 
 * @author zzw
 */


@Configuration
@ConditionalOnProperty(value = "spring.isoaconfig.druid.enabled", havingValue = "true", matchIfMissing = true)
public class DruidConfig {
	
	@Value("${spring.druid.allow:127.0.0.1}")
	private String allow;
	@Value("${spring.druid.deny:192.0.0.1}")
	private String deny;
	@Value("${spring.druid.loginUsername:admin}")
	private String loginUsername;
	@Value("${spring.druid.loginPassword:admin}")
	private String loginPassword;
	@Value("${spring.druid.resetEnable:false}")
	private String resetEnable;
	
	@Value("${spring.druid.urlPatterns:/*}")
	private String urlPatterns;
	@Value("${spring.druid.exclusions:*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid2/*}")
	private String exclusions;
	@Value("${spring.druid.order:10}")
	private Integer order; 
	
	
	
	/**
	 * 注册一个StatViewServlet
	 * 
	 * @return
	 */
	@Bean
	public ServletRegistrationBean DruidStatViewServle2() {
		ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),
				"/druid/*");
		// 添加初始化参数：initParams
		/** 白名单，如果不配置或value为空，则允许所有 */
		//servletRegistrationBean.addInitParameter("allow", allow);//默认"127.0.0.1,192.0.0.1"
		/** 黑名单，与白名单存在相同IP时，优先于白名单 */
		//servletRegistrationBean.addInitParameter("deny", deny);//默认192.0.0.1
		/** 用户名 */
		servletRegistrationBean.addInitParameter("loginUsername", loginUsername);//默认admin
		/** 密码 */
		servletRegistrationBean.addInitParameter("loginPassword", loginPassword);//默认admin
		/** 禁用页面上的“Reset All”功能 */
		servletRegistrationBean.addInitParameter("resetEnable", resetEnable);//默认false
		return servletRegistrationBean;
	}

	/**
	 * 注册一个：WebStatFilter
	 * 
	 * @return
	 */
	@Bean
	public FilterRegistrationBean druidStatFilter2() {
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());

		/** 过滤规则 */
		filterRegistrationBean.addUrlPatterns(urlPatterns); //默认"/*"
		/** 忽略资源 */
		filterRegistrationBean.addInitParameter("exclusions", exclusions); //默认"*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid2/*"
		filterRegistrationBean.setOrder(order);//默认10
		return filterRegistrationBean;
	}

}
